<template>
  <!-- 专辑详情 -->
  <div class="mod-ald">
    <div class="container" v-if="code == 200">
      <div class="ald-hd">
        <div class="ald-hd-pic">
          <img :src="albumDetail.picUrl" alt="" class="ald-pic" />
        </div>
        <div class="ald-hd-main">
          <div class="ald-title-box">
            <h1 class="ald-title">
              <a href="javascript:;" :title="albumDetail.name">{{
                albumDetail.name
              }}</a>
            </h1>
          </div>

          <div class="ald-artist-box">
            <h4 class="ald-artist">
              歌手：
              <a
                v-for="(art, n) in albumDetail.artists"
                :key="art.id"
                href="javascript:;"
                @click="clickArtist(art.id)"
                :title="art.name"
              >
                <i v-if="n > 0"> / </i>
                {{ art.name }}
              </a>
            </h4>
          </div>
          <div class="ald-publish-box">
            <div class="ald-publish">
              发行时间：{{ albumDetail.publishTime | showDate }}
            </div>
            <div class="ald-company" v-if="albumDetail.company">
              发行公司：{{ albumDetail.company }}
            </div>
          </div>

          <div class="mod-songlist-toolbar">
            <a class="mod-btn-blue">
              <span class="btn-play" @click="playAll">
                <i class="iconfont">&#xe8a8;</i>
                全部播放
              </span>
              <span class="btn-add">
                <i class="iconfont" @click="addAll">&#xe603;</i>
              </span>
            </a>
            <a class="mod-btn" @click="clickCol">
              <i class="iconfont">&#xe8c8;</i>
              收藏({{ albumInfo.subCount }})
            </a>
            <a class="mod-btn">
              <i class="iconfont">&#xe8a6;</i>
              下载
            </a>
            <a class="mod-btn" @click="clickShare">
              <i class="iconfont">&#xe8af;</i>
              分享({{ albumInfo.shareCount }})
            </a>
            <a class="mod-btn">
              <i class="iconfont">&#xe8a7;</i>
              评论（{{ commentData.total }}）
            </a>
          </div>
        </div>
      </div>

      <div class="ald-relative-box">
        <h2 class="ald-relative-hd">专辑介绍</h2>
        <p :class="['ald-description', { limit: isLimit }]">
          {{ albumDetail.description }}
        </p>
        <a class="ald-description-height" @click="changeLimit">{{ show }}</a>
      </div>

      <div class="ald-left">
        <div class="ald-list">
          <div class="songlist-message">
            <h3>歌曲列表</h3>
            <span class="songlist-num">{{ songs.length }}首歌</span>
          </div>
          <art-song-list
            :songs="songs"
            :showArtist="true"
            ref="songlist"
          ></art-song-list>
        </div>
        <div class="ald-comment" v-if="commentData">
          <comment ref="comment" :commentData="commentData"></comment>
          <div class="comment-el-pagination" v-if="commentData.total > 20">
            <el-pagination
              background
              @current-change="handleCurrentChangeC"
              :current-page.sync="currentPageC"
              :page-size="20"
              layout="prev, pager, next, jumper"
              :total="commentData.total"
            >
            </el-pagination>
          </div>
        </div>
      </div>

      <div class="ald-relative">
        <!-- <div class="ald-relative-box">
          <h2 class="ald-relative-hd">歌单收藏者</h2>
          <ul class="ald-collector clearfix">
            <li
              class="ald-collector-item"
              v-for="item in collectors"
              :key="item.userId"
            >
              <a href="javascript:;" :title="item.nickname">
                <img :src="item.avatarUrl" :alt="item.nickname" />
              </a>
            </li>
          </ul>
        </div> -->
        <div class="ald-relative-box">
          <h2 class="ald-relative-hd">
            Ta的其他热门专辑
            <a href="javascript:;" class="ald-relative-hd-more">
              全部
              <i class="el-icon-arrow-right"></i>
            </a>
          </h2>
          <div class="realbum-box" v-for="item in otherAlbums" :key="item.id">
            <div class="left">
              <a href="javascript:;" @click="clickAlbumDetail(item.id)">
                <img :src="item.picUrl" :alt="item.name" class="realbum-pic" />
              </a>
            </div>
            <h4 class="realbum-name">
              <a
                href="javascript:;"
                :title="item.name"
                @click="clickAlbumDetail(item.id)"
                >{{ item.name }}</a
              >
            </h4>
            <div class="realbum-time">
              {{ item.publishTime | showDate }}
            </div>
          </div>
        </div>
      </div>
    </div>
    <small-tip :tipMessage="smallTipMsg" v-if="showSmallTip"></small-tip>
    <no-resource v-if="code == 404"></no-resource>
  </div>
</template>

<script>
import { formatDate } from "common/date.js";
import Comment from "views/toplists/child/Comment.vue";
import ArtSongList from "components/songlist/ArtSongList.vue";
import SmallTip from "components/tip/SmallTip";
import NoResource from "views/notfound/NoResource";

export default {
  components: {
    Comment,
    ArtSongList,
    SmallTip,
    NoResource,
  },
  data() {
    return {
      albumDetail: {},
      songs: [],
      albumInfo: {},
      currentPageC: 1,
      commentData: {},
      isLimit: true,
      show: "[展开]",
      id: "",
      otherAlbums: [],
      showSmallTip: false,
      smallTipMsg: "操作提示",
      code: 200,
    };
  },
  filters: {
    // 过滤器转换时间戳
    showDate(value) {
      const date = new Date(value);
      return formatDate(date, "yyyy-MM-dd");
    },
  },
  mounted() {
    this.id = this.$route.query.id;
    this.getAlbumDetail();
    this.getComment();
    this.getAlbumInfo();
  },
  methods: {
    clickShare() {
      if (this.$store.state.isLogin) {
        let newItem = {
          id: this.albumDetail.id,
          type: "album",
          name: this.albumDetail.name,
        };
        this.albumDetail.artists.forEach((item, index) => {
          if (index == 0) {
            newItem.artist = item.name;
          } else {
            newItem.artist += "/" + item.name;
          }
        });
        // console.log(newItem);
        this.$store.commit("changeShowShare", true);
        this.$store.commit("changeShareItem", newItem);
      } else {
        this.smallTipMsg = "请登录后再分享";
        this.showSmallTip = true;
        setTimeout(() => {
          this.showSmallTip = false;
        }, 1000);
      }
    },
    clickCol() {
      if (this.$store.state.isLogin) {
        var newItem = [];
        this.songs.forEach((item) => {
          newItem[newItem.length] = item.id;
        });
        let ids = newItem.join(",");
        this.$store.commit("changeShowCollect", true);
        this.$store.commit("changeSId", ids);
      } else {
        this.smallTipMsg = "请登录后再收藏";
        this.showSmallTip = true;
        setTimeout(() => {
          this.showSmallTip = false;
        }, 1000);
      }
    },
    playAll() {
      var newItem = [];
      this.songs.forEach((item) => {
        newItem[newItem.length] = {
          id: item.id,
          name: item.name,
          ar: item.ar,
          al: item.al,
          mv: item.mv,
          dt: item.dt,
          fee: item.fee,
        };
      });
      this.playSongs(newItem, true);
    },
    addAll() {
      var newItem = [];
      this.songs.forEach((item) => {
        newItem[newItem.length] = {
          id: item.id,
          name: item.name,
          ar: item.ar,
          al: item.al,
          mv: item.mv,
          dt: item.dt,
          fee: item.fee,
        };
      });
      console.log(newItem);
      this.addSongs(newItem, true);
    },
    clickArtist(id) {
      this.playArtistDetail(id);
    },
    clickAlbumDetail(id) {
      this.playAlbumDetail(id);
    },
    getOtherAlbum(artid) {
      this.axios({
        /* 歌手专辑 */
        url: "/artist/album?limit=5&id=" + artid,
      })
        .then((res) => {
          this.otherAlbums = res.data.hotAlbums;
          // console.log(this.otherAlbums);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    changeLimit() {
      this.isLimit = !this.isLimit;
      if (this.isLimit) {
        this.show = "[展开]";
        document.documentElement.scrollTo({
          top: 141,
          behavior: "smooth",
        });
      } else {
        this.show = "[收起]";
      }
    },
    getAlbumInfo() {
      /* 专辑收藏分享数 */
      this.axios({
        url: "/album/detail/dynamic?id=" + this.id,
      })
        .then((res) => {
          this.albumInfo = res.data;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    handleCurrentChangeC() {
      this.getComment();
      // 点击分页后，滚动到评论的头部，141为页面头部组件的高度
      document.documentElement.scrollTo({
        top: this.$refs.comment.$el.offsetTop + 141,
        behavior: "smooth",
      });
    },
    getComment() {
      this.axios({
        /* 歌单评论接口地址 */
        url:
          "/comment/album?id=" +
          this.id +
          "&offset=" +
          (this.currentPageC - 1) * 20,
      })
        .then((res) => {
          this.commentData = res.data;
          console.log(res.data);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    getAlbumDetail() {
      this.axios({
        url: "/album?id=" + this.id,
      })
        .then((res) => {
          if (res.data.code == 200) {
            this.code = 200;
            this.albumDetail = res.data.album;
            this.songs = res.data.songs;
            this.getOtherAlbum(res.data.album.artist.id);
            console.log(this.albumDetail);
            console.log(this.songs);
          }
          if (res.data.code == 404) {
            this.code = 404;
          }
        })
        .catch((err) => {
          console.log(err);
          this.code = 404;
        });
    },
  },
};
</script>

<style>
.mod-ald {
  padding-top: 50px;
  background-color: var(--color-theme-light-5);
}
.ald-hd {
  position: relative;
  height: 250px;
  margin-bottom: 50px;
}
.ald-hd-pic {
  position: absolute;
  left: 0;
  top: 0;
  width: 250px;
  height: 250px;
}
.ald-pic {
  width: 100%;
  height: 100%;
}
/* song-hd-main */
.ald-hd-main {
  position: relative;
  margin-left: 300px;
  height: 250px;
  overflow: hidden;
}
.ald-title,
.ald-artist {
  font-weight: 400;
}
.ald-title-box {
  height: 50px;
  line-height: 50px;
}
.ald-artist-box {
  height: 50px;
  line-height: 20px;
}
.ald-artist a:hover {
  color: var(--color-theme);
}
.ald-publish-box {
  line-height: 40px;
}
/* mod-songlist-toolbar */
.mod-ald .mod-songlist-toolbar {
  position: absolute;
  bottom: 0;
}
.mod-songlist-toolbar .mod-btn,
.mod-songlist-toolbar .mod-btn-blue {
  min-width: 122px;
  text-align: center;
}
.mod-songlist-toolbar .iconfont {
  width: 13px;
  height: 16px;
}
/* ald-left */
.ald-left {
  float: left;
  width: 850px;
}
/* 歌曲列表头 */
.songlist-message {
  height: 50px;
  line-height: 50px;
}
.songlist-message h3 {
  float: left;
  font-weight: 400;
  margin-right: 15px;
}
.songlist-message .songlist-num {
  font-size: 14px;
  color: #999;
}
.songlist-message .songlist-play {
  float: right;
  margin-right: 20px;
  font-size: 14px;
}
.songlist-message .songlist-play .playcount {
  color: var(--color-theme);
}
.comment-el-pagination {
  position: relative;
  text-align: center;
  margin: 20px auto;
}
.ald-relative {
  float: right;
  width: 300px;
  overflow: hidden;
}
.ald-relative-box {
  position: relative;
  width: 100%;
  overflow: hidden;
}
.ald-relative-hd {
  position: relative;
  margin-bottom: 10px;
  font-weight: 400;
  height: 36px;
  line-height: 36px;
}
.ald-relative-hd-more {
  position: absolute;
  right: 0;
  bottom: -5px;
  font-size: 14px;
}
.ald-relative-hd-more:hover {
  color: var(--color-theme);
}
.ald-description {
  width: 100%;
  font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB",
    "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
  font-size: 14px;
  line-height: 26px;
  margin-bottom: 20px;
  white-space: pre-wrap;
  white-space: -moz-pre-wrap;
  white-space: -pre-wrap;
  white-space: -o-pre-wrap;
  /* *word-wrap: break-word; */
  /* *white-space: normal; */
}
.ald-description.limit {
  max-height: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
}
.ald-description-height {
  float: right;
  font-size: 14px;
  color: var(--color-theme);
  cursor: pointer;
  margin-bottom: 20px;
}
/* .ald-collector {
  width: 100%;
  margin-bottom: 20px;
}
.ald-collector-item {
  float: left;
  width: 65px;
  height: 65px;
  margin-right: 10px;
  margin-bottom: 10px;
}
.ald-collector-item a {
  display: block;
  width: 65px;
  height: 65px;
}
.ald-collector-item img {
  width: 65px;
  height: 65px;
} */

.realbum-box {
  margin-left: 80px;
  vertical-align: middle;
  height: 70px;
}
.realbum-box .left {
  display: inline;
  margin-top: 5px;
  margin-left: -75px;
  overflow: hidden;
}
.realbum-box .left a {
  position: relative;
  display: block;
  width: 60px;
  height: 60px;
}
.realbum-pic {
  width: 60px;
  height: 60px;
}
.realbum-name,
.realbum-author,
.realbum-creator {
  font-weight: 400;
  padding-top: 10px;
  overflow: hidden;
  text-overflow: ellipsis;
}
.realbum-name a,
.realbum-author a,
.realbum-creator a {
  white-space: nowrap;
}
.realbum-name a:hover,
.realbum-author a:hover,
.realbum-creator a:hover {
  color: var(--color-theme);
}
.realbum-time {
  margin-top: 10px;
  font-size: 14px;
  color: #999;
}
</style>